#! /usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use Pod::Usage; # use POD for Usage



my %options=(
			sid=>undef,
			servername=>"iospbc01.net120.os.itelligence.de",
			clientname=>undef,
			pool=>"ddpool",	   
			sqlname=>"MSSQL:",
			named=>undef,
			db=>undef,
			retent=>"<Number of Weeks>",
			oracle_version=>"<VERSION>",
			lang=>"<LANG>",
			db2name=>undef,
			db2path=>undef,
			sessions=>1,
			insthome=>undef,
			flag_help=>undef, # declare flag for help display
			max_user=>'control',
			max_user_password=>undef,
			dbmcli_bin=>undef,
			nr_ignores_force=>undef,
			nr_ignores=>1,
			nr_wait=>2,
			db2lib=>'/usr/lib/libnsrdb2.so',
			sqlname=>undef,
			nsr_res_path=>'',
			file_extension=>'sh',
			env_oracle_sid=>'\'$ORACLE_SID\'',
			db2_backup_string=>'',
			lowercase_sid=>'',
			username=>'',
			SAPDATA_HOME=>'',
			ORACLE_HOME=>'',
			DIR_LIBRARY=>'',
			NLS_LANG=>'',
			DB2DIR=>'',
			INSTHOME=>'',
			dbuser_password=>'',
			initial=>undef
);

my @files;

GetOptions("server=s"=>\$options{servername},
	   "client=s"=>\$options{clientname},
	   "sid=s"=>\$options{sid},
	   "pool=s"=>\$options{pool},
	   "named"=>\$options{named},
	   "db=s"=>\$options{database},
	   "retent=s"=>\$options{retent},
	   "ora=s"=>\$options{oracle_version},
	   "lang=s"=>\$options{lang},
	   "db2name=s"=>\$options{db2name},
	   "db2path=s"=>\$options{db2path},
	   "sessions=s"=>\$options{sessions},
	   "insthome=s"=>\$options{insthome},
	   "help|?"=>\$options{flag_help},
	   "username=s"=>\$options{max_user},
	   "password=s"=>\$options{max_user_password},
	   "dbmcli=s"=>\$options{dbmcli_bin},
	   "nr_ignores=s"=>\$options{nr_ignores},
	   "nr_ignores_force=s"=>\$options{nr_ignores_force},
	   "nr_wait=s"=>\$options{nr_wait},
	   "dbuserpass=s"=>\$options{dbuser_password},
	   "initial"=>\$options{initial}
);

&pod2usage(verbose=>2) if ($options{flag_help}); # display usage info if help flag is defined
$options{lowercase_sid}=lc($options{sid});

if ($^O =~ /Win/){
	if (not $options{dbmcli_bin}){$options{dbmcli_bin}="E:/sapdb/programs/pgm/dbmcli.exe"}
	$options{nsr_res_path}='C:\\Legato\\nsr\\res\\';
	$options{file_extension}='cmd';
	$options{env_oracle_sid}='%ORACLE_SID%';
	$options{db2_backup_string}="set DB2CLP=DB20FADE\ndb2 backup db ".$options{sid}." online load \"C:\\Legato\\nsr\\bin\\libnsrdb2.dll\" open ".$options{sessions}." sessions options \"\@".$options{nsr_res_path}."nmdb2_".$options{sid}.".cfg\"\npause";
	
} else {
	if (not $options{dbmcli_bin}){$options{dbmcli_bin}="/sapdb/programs/bin/dbmcli"}
	$options{nsr_res_path}='/nsr/res/';
	if ($^O =~ /aix/){$options{db2lib}="/usr/lib/libnsrdb2.o"}
	$options{db2_backup_string}="db2 backup db ".$options{sid}." online load '".$options{db2lib}."' open ".$options{sessions}." sessions options '@".$options{nsr_res_path}."nmdb2_".$options{sid}.".cfg'";
	
}

if ($options{named}) { $options{sqlname}="MSSQL\$".$options{sid}.":\""}


if ($options{database} eq "MSSQL"){ 
	open(FILE,">$options{nsr_res_path}Backup_DB_".$options{sid}.".$options{file_extension}");
	print FILE "set client=".$options{clientname}."\n";
	print FILE "set server=".$options{servername}."\n";
	print FILE "set networker_bin=C:\\Legato\\nsr\\bin\\\n";
	print FILE "set sqlname=\"".$options{sqlname}."\n";
	print FILE "\"%networker_bin%\\nsrsqlsv.exe\" -s %server% -b ".$options{pool}." -m %client% -l full -q -N %sqlname% %sqlname%\n";
	print FILE "pause\n";

	close(FILE);

	open(FILE,">$options{nsr_res_path}Backup_LOG_".$options{sid}.".$options{file_extension}");
	print FILE "set client=".$options{clientname}."\n";
	print FILE "set server=".$options{servername}."\n";
	print FILE "set networker_bin=C:\\Legato\\nsr\\bin\\\n";
	print FILE "set sqlname=\"".$options{sqlname}."\n";
	print FILE "\"%networker_bin%\\nsrsqlsv.exe\" -h master -h model -h msdb -h tempdb -s %server% -b LOG -m %client% -l incr -q -N %sqlname% %sqlname%\n";
	print FILE "pause\n";

	close(FILE);
}

elsif ($options{database} eq "SAP"){
	
		open(FILE,">$options{nsr_res_path}Brarchive_CDS_".$options{sid}.".$options{file_extension}");
		push(@files,"$options{nsr_res_path}Brarchive_CDS_".$options{sid}.".$options{file_extension}");
		print FILE "brarchive -cds -d util_file -r ".$options{nsr_res_path}."init$options{env_oracle_sid}_LOG.utl -u / -c\n";
		close(FILE);
				
		open(FILE,">$options{nsr_res_path}Brarchive_SD_".$options{sid}.".$options{file_extension}");
		push(@files,"$options{nsr_res_path}Brarchive_SD_".$options{sid}.".$options{file_extension}");
		print FILE "brarchive -sd -d util_file -r ".$options{nsr_res_path}."init$options{env_oracle_sid}_LOG.utl -u / -c\n";
		close(FILE);
				
		open(FILE,">$options{nsr_res_path}Brbackup_ONLINE_".$options{sid}.".$options{file_extension}");
		push(@files,"$options{nsr_res_path}Brbackup_ONLINE_".$options{sid}.".$options{file_extension}");
		print FILE "brbackup -m all -t online_cons -d util_file_online -r ".$options{nsr_res_path}."init$options{env_oracle_sid}_DB.utl -u / -c\n";
		close(FILE);

		open(FILE,">$options{nsr_res_path}init".$options{sid}."_DB.utl");
		print FILE "savesets = 50\n";
		print FILE "parallelism = 2\n";
		print FILE "pool = ".$options{pool}."\n";
		print FILE "server = ".$options{servername}."\n";
		print FILE "client = ".$options{clientname}."\n";
		print FILE "NSR_SAVESET_RETENTION = ".$options{retent}."\n";
		print FILE "NSR_SAVESET_BROWSE = ".$options{retent}."\n";
		close(FILE);
	
	
		open(FILE,">$options{nsr_res_path}init".$options{sid}."_LOG.utl");
		print FILE "savesets = 6\n";
		print FILE "parallelism = 2\n";
		print FILE "pool = LOG\n";
		print FILE "server = ".$options{servername}."\n";
		print FILE "client = ".$options{clientname}."\n";
		print FILE "NSR_SAVESET_RETENTION = ".$options{retent}."\n";
		print FILE "NSR_SAVESET_BROWSE = ".$options{retent}."\n";
		close(FILE);
				
	if ($^O =~ /linux/  || $^O =~ /aix/ ){
		$options{username}="ora$options{lowercase_sid}";
						
		my @environment =`su - $options{username} -c "env"`;
		foreach (@environment){
			chomp @environment;
			my($key,$value)=split(/\s*=\s*/);
			$options{$key}=$value;
		}
		
		open(FILE,">$options{nsr_res_path}nsrsapsv_$options{sid}_log_cds.cfg");
		print FILE "BR_EXEC=brarchive -cds -d util_file -r ".$options{nsr_res_path}."init".$options{sid}."_LOG.utl -u /\n";
		print FILE "ORACLE_HOME=$options{ORACLE_HOME}\n";
		print FILE "SAP_BIN=$options{DIR_LIBRARY}\n";
		print FILE "SAPDATA_HOME=$options{SAPDATA_HOME}\n";
		print FILE "NLS_LANG=$options{NLS_LANG}\n";
		print FILE "DIR_LIBRARY=$options{DIR_LIBRARY}\n";
		print FILE '###';
		print FILE " set password for orasid  nsrsapadm -P -z ".$options{nsr_res_path}."nsrsapsv*_log_cds.cfg ";
		print FILE '###';
		print FILE "\n";
		close(FILE);

	
		open(FILE,">$options{nsr_res_path}nsrsapsv_$options{sid}_online.cfg");
		print FILE "BR_EXEC=brbackup -m all -t online_cons -d util_file_online -r ".$options{nsr_res_path}."init".$options{sid}."_DB.utl -u /\n";
		print FILE "ORACLE_HOME=$options{ORACLE_HOME}\n";
		print FILE "SAP_BIN=$options{DIR_LIBRARY}\n";
		print FILE "SAPDATA_HOME=$options{SAPDATA_HOME}\n";
		print FILE "NLS_LANG=$options{NLS_LANG}\n";
		print FILE "DIR_LIBRARY=$options{DIR_LIBRARY}\n";
		print FILE '###';
		print FILE " set password for orasid  nsrsapadm -P -z ".$options{nsr_res_path}."nsrsapsv*_online.cfg ";
		print FILE '###';
		print FILE "\n";
		close(FILE);

	}
	
	elsif ($^O =~ /MSWin32/){
				
		open(FILE,">$options{nsr_res_path}nsrsapsv_$options{sid}_log_cds.cfg");
		print FILE "BR_EXEC=brarchive -cds -d util_file -r ".$options{nsr_res_path}."init".$options{sid}."_LOG.utl -u /\n";
		print FILE "ORACLE_HOME=E:\\oracle\\".$options{sid}."\\".$options{oracle_version}."\n";
		print FILE "SAP_BIN=E:\\usr\\sap\\".$options{sid}."\\SYS\\exe\\run\n";
		print FILE "SAPDATA_HOME=F:\\oracle\\".$options{sid}."\n";
		print FILE "NLS_LANG=AMERICAN_AMERICA.".$options{lang}."\n";
		print FILE "DIR_LIBRARY=E:\\usr\\sap\\".$options{sid}."\\SYS\\exe\\run\n";
		print FILE "SAPBACKUP=O:\\oracle\\".$options{sid}."\\sapbackup\n";
		print FILE "SAPARCH=O:\\oracle\\".$options{sid}."\\saparch\n";
		print FILE "SAPREORG=O:\\oracle\\".$options{sid}."\\sapreorg\n";
		print FILE "SAPTRACE=O:\\oracle\\".$options{sid}."\\saptrace\n";
		print FILE "SAPCHECK=O:\\oracle\\".$options{sid}."\\sapcheck\n";
		print FILE '###';
		print FILE " set password for orasid  nsrsapadm -P -z ".$options{nsr_res_path}."nsrsapsv*_log_cds.cfg ";
		print FILE '###';
		print FILE "\n";
		close(FILE);
	
	
		open(FILE,">$options{nsr_res_path}nsrsapsv_$options{sid}_online.cfg");
		print FILE "BR_EXEC=brbackup -m all -t online_cons -d util_file_online -r ".$options{nsr_res_path}."init".$options{sid}."_DB.utl -u /\n";
		print FILE "ORACLE_HOME=E:\\oracle\\".$options{sid}."\\".$options{oracle_version}."\n";
		print FILE "SAP_BIN=E:\\usr\\sap\\".$options{sid}."\\SYS\\exe\\run\n";
		print FILE "SAPDATA_HOME=F:\\oracle\\".$options{sid}."\n";
		print FILE "NLS_LANG=AMERICAN_AMERICA.".$options{lang}."\n";
		print FILE "DIR_LIBRARY=E:\\usr\\sap\\".$options{sid}."\\SYS\\exe\\run\n";
		print FILE "SAPBACKUP=O:\\oracle\\".$options{sid}."\\sapbackup\n";
		print FILE "SAPARCH=O:\\oracle\\".$options{sid}."\\saparch\n";
		print FILE "SAPREORG=O:\\oracle\\".$options{sid}."\\sapreorg\n";
		print FILE "SAPTRACE=O:\\oracle\\".$options{sid}."\\saptrace\n";
		print FILE "SAPCHECK=O:\\oracle\\".$options{sid}."\\sapcheck\n";
		print FILE '###';
		print FILE " set password for orasid  nsrsapadm -P -z ".$options{nsr_res_path}."nsrsapsv*_log_cds.cfg ";
		print FILE '###';
		print FILE "\n";
		close(FILE);
		
	}
	
	open(SETORAPASS, "| nsrsapadm -P -z $options{nsr_res_path}nsrsapsv_$options{sid}_log_cds.cfg");
	print SETORAPASS "$options{username}\n";
	print SETORAPASS "$options{dbuser_password}\n";
	print SETORAPASS "$options{dbuser_password}\n";
	print SETORAPASS "N\n";
	close(SETORAPASS);
	
	open(SETORAPASS, "| nsrsapadm -P -z $options{nsr_res_path}nsrsapsv_$options{sid}_online.cfg");
	print SETORAPASS "$options{username}\n";
	print SETORAPASS "$options{dbuser_password}\n";
	print SETORAPASS "$options{dbuser_password}\n";
	print SETORAPASS "N\n";
	close(SETORAPASS);	
	
	if ($options{initial}){
	if ($^O =~ /linux/){
	`/bin/chown root:root /usr/sbin/backint`;
	`/bin/chmod 4755 /usr/sbin/backint`;
	`ln -s /usr/sbin/backint $options{DIR_LIBRARY}/backint`;
	}
	if ($^O =~ /aix/){
	`/bin/chown root:system /usr/bin/backint`;
	`/bin/chmod 4755 /usr/bin/backint`;
	`ln -s /usr/bin/backint $options{DIR_LIBRARY}/backint`;
	}}
	
}
elsif ($options{database} eq "DB2"){
	if (not $options{db2name}){$options{db2name}="db2".$options{lowercase_sid};}
	$options{username}=$options{db2name};
		
	my @db2environment =`su - $options{username} -c "env"`;
	foreach (@db2environment){
		chomp @db2environment;
		my($db2key,$db2value)=split(/\s*=\s*/);
		$options{$db2key}=$db2value;
	}
		
		
	open(FILE,">$options{nsr_res_path}nmdb2_$options{sid}.cfg");
	print FILE "NSR_SERVER = ".$options{servername}."\n";
	print FILE "NSR_CLIENT = ".$options{clientname}."\n";
	print FILE "NSR_DATA_VOLUME_POOL = ".$options{pool}."\n";
	print FILE "NSR_LOG_VOLUME_POOL = LOG\n";
	print FILE "NSR_SAVESET_BROWSE = ".$options{retent}."\n";
	print FILE "NSR_SAVESET_RETENTION = ".$options{retent}."\n";
	print FILE "NSR_DR_BACKUP_INFO = TRUE\n\n\n";
	print FILE "DB2INSTANCE = ".$options{db2name}."\n";
	print FILE "DB2_ALIAS = ".$options{sid}."\n";
	print FILE "DB2_INSTALL_PATH = $options{DB2DIR}\n";
	print FILE "DB2_NODE_NAME = ".$options{db2name}."\n";
	print FILE "DB2_OPTIONS = DB2BACKUP_DB, DB2BACKUP_FULL, DB2BACKUP_ONLINE, DB2BACKUP_INCLUDE_LOGS\n";
	print FILE "DB2_SESSIONS = ".$options{sessions}."\n";
	print FILE "DB2_USER = ".$options{db2name}."\n";
	print FILE "INSTHOME = $options{INSTHOME}\n\n";
	print FILE '# To use this configuration file for scheduled backup, specify the database user password by running the command "nsrdaadmin -P -z configuration_file"';
	print FILE "\n";
	close(FILE);
	open(FILE,">$options{nsr_res_path}Backup_".$options{sid}."_online.$options{file_extension}");
	if ($options{file_extension} eq "sh"){push(@files,"$options{nsr_res_path}Backup_".$options{sid}."_online.$options{file_extension}")}
	print FILE $options{db2_backup_string};
	close(FILE);
	
	open(SETDB2PASS, "| nsrdaadmin -P -z $options{nsr_res_path}nmdb2_$options{sid}.cfg");
	print SETDB2PASS "$options{dbuser_password}\n";
	print SETDB2PASS "$options{dbuser_password}\n";
	close(SETDB2PASS);	
	
}
elsif ($options{database} eq "MAX"){
	$options{username}="$options{lowercase_sid}adm";
	
	open(FILE,">$options{nsr_res_path}nsrMaxDB_$options{sid}.cfg");
	print FILE "nr_ignores= ".$options{nr_ignores}.' # times to ignore LOG run while db backup is running'."\n";
	print FILE "nr_wait= ".$options{nr_wait}.' # time to wait for db backup for running log backup - wait x * 5min , max. 1 hours'."\n";
	print FILE "user= ".$options{max_user}.' # user for connection to database'."\n";
	print FILE "password= ".$options{max_user_password}."\n";
	print FILE '# dbmcli binary - use "/" for paths also in Windows!'."\n";
	print FILE '# i.e. "/sapdb/programs/bin/dbmcli" or "E:/sapdb/programs/pgm/dbmcli.exe"'."\n";
	print FILE "dbmcli_bin= ".$options{dbmcli_bin}.' # path of dbmcli'."\n";
	if ($options{nr_ignores_force}){print FILE "nr_ignores_force= ".$options{nr_ignores_force}.' # Number of times logbackup is going to wait for running db backup - beware that max is 12'."\n"}
	close FILE;
	
	open(FILE,">$options{nsr_res_path}Backup_DB_$options{sid}.$options{file_extension}");
	($^O =~ /MSWin32/) ? print FILE '@echo off'."\n" : push(@files,"$options{nsr_res_path}Backup_DB_$options{sid}.$options{file_extension}"); 
	print FILE 'C:/Nagios/perl510/perl.exe C:/Legato/nsr/res/nsrMaxDB_backup.pl '."-t DB -s $options{sid}\n";
	if ($^O =~ /MSWin32/){print FILE "pause\n"}
	close FILE;
	
	open(FILE,">$options{nsr_res_path}Backup_LOG_$options{sid}.$options{file_extension}");
	($^O =~ /MSWin32/) ? print FILE '@echo off'."\n" : push(@files,"$options{nsr_res_path}Backup_LOG_$options{sid}.$options{file_extension}"); 
	print FILE 'C:/Nagios/perl510/perl.exe C:/Legato/nsr/res/nsrMaxDB_backup.pl '."-t LOG -s $options{sid}\n";
	if ($^O =~ /MSWin32/){print FILE "pause\n"}
	close FILE;
	
}

##### set File Permissions and Links on Unix

if (not $^O =~ /MSWin32/) {
	chmod(0755,@files);
	`su - $options{username} -c "ln -s $options{nsr_res_path}B* ./"`;
	
}


__END__

=pod

=head1 VERSION 1.0

=head1 NAME deploy_conf.pl

=head1 DESCRIPTION

The script creates configfiles according to the provided parameters

=head1 SYNOPSIS

B<deploy_conf.pl> -server <nw-server> -client <client> -sid <SID> -pool <POOL> -named <MSSQL named instance ?> -db <MSSQL/SAP> -retent <retention> -ora <oracle-version> -lang <db-language> -os <operating system>

B<deploy_conf.pl> -help

=head1 OPTIONS

=over 4

=item -server <networker-server-name>

=item -client <clientname>

=item -sid <SID>

=item -pool <POOL>

=item -named <named instance: yes - only required if so>

=item -db <Type of database - MSSQL/SAP>

=item -retent <retention - 2Weeks/4Weeks/35Days>

=item -ora <oracle-version 112_64/11202 needed for oracle Dir>

=item -lang <db-language UTF8 or WE8DEC>

=item -os <operating system Linux/AIX/Win>

=item -help                     displays help

=back

=cut
